package com.blkj.iam.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blkj.iam.common.util.ClientUtils;
import com.blkj.iam.core.security.util.SecurityUtils;
import com.blkj.iam.system.converter.ClientConverter;
import com.blkj.iam.system.mapper.ClientMapper;
import com.blkj.iam.system.model.entity.Client;
import com.blkj.iam.system.model.form.ClientForm;
import com.blkj.iam.system.model.query.ClientPageQuery;
import com.blkj.iam.system.model.vo.ClientPageVO;
import com.blkj.iam.system.service.ClientService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 客户端表
 * @Author: jeecg-boot
 * @Date: 2025-04-09
 * @Version: V1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientService {

    private final ClientConverter clientConverter;
    private final ClientMapper clientMapper;

    @Override
    public IPage<ClientPageVO> getClientPage(ClientPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Client> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<Client> tenantPage = this.page(page, new LambdaQueryWrapper<Client>().like(ObjectUtil.isNotEmpty(queryParams.getAppName()), Client::getAppName, queryParams.getAppName()));

        // 实体转换
        return clientConverter.toPageVo(tenantPage);
    }

    @Override
    public ClientForm getClientFormData(Long clientId) {
        Client client = this.getById(clientId);
        return clientConverter.toForm(client);
    }

    @Override
    public boolean saveClient(ClientForm clientForm) {
        // 实体转换 form->entity
        Client entity = clientConverter.toEntity(clientForm);
        entity.setCreateBy(SecurityUtils.getUserId());
        String appId = ClientUtils.generateAppId();
        String appKey = ClientUtils.generateAppKey();
        entity.setAppId(appId);
        entity.setAppKey(appKey);
        return this.save(entity);
    }

    @Override
    public boolean updateClient(Long clientId, ClientForm clientForm) {
        // 实体转换 form->entity
        Client entity = clientConverter.toEntity(clientForm);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }

    @Override
    public boolean deleteClients(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的应用数据为空");
        // 将字符串 ID 列表转换为 Long 类型的列表
        List<Long> tenantIds = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return clientMapper.deleteByIds(tenantIds) > 0;
    }
}
